package test;

import bean.Emp;
import mapper.EmpMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class App {
    private static EmpMapper mapper;
    private static EmpMapper mapper2;
    private static SqlSession session;
    private static SqlSession session2;

    static {
        try {
            //加载配置文件
            InputStream in = Resources.getResourceAsStream("config.xml");
            //创建会话工厂
            SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
            //创建会话
            session = ssf.openSession();
            //获取代理对象
            mapper = session.getMapper(EmpMapper.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void a(){
        List<Emp> list = mapper.findAllEmp();
        for (Emp emp:
             list) {
            System.out.println(emp);
        }
    }

    public static void b(){
        Emp emp=new Emp("张两千","设计师",7960,8000.0,4000.0,10);
        mapper.saveEmp(emp);
        session.commit();
    }

    public static void c(){
        Emp emp=new Emp(7961,null,8800.0);
        mapper.updateEmp(emp);
        session.commit();
    }

    public static void d(){
        mapper.deleteEmp(7961);
        session.commit();
    }

    public static void e(){
        Emp emp=mapper.find1(7959);
        System.out.println(emp);
    }

    public static void f() {
        Emp emp = mapper.find2("朱明辉");
        session.close();
        Emp emp2 = mapper2.find2("朱明辉");
        session2.close();
        System.out.println(emp);
        System.out.println(emp2);
    }
    public static void main(String[] args) {
//        a();
//        b();
//        c();
//        d();
        e();
    }
}
